#!/bin/sh
#
# Get the supplementary group ID(s) needed to access optical drive(s).
#

set -e # Exit immediately if a command exits with a non-zero status.
set -u # Treat unset variables as an error.

GRPS=$(mktemp)

lsscsi -k | grep -w "cd/dvd" | tr -s ' ' | while read -r DRV
do
    SR_DEV="$(echo "$DRV" | rev | awk '{print $1}' | rev)"

    # Save the associated group.
    if [ -e "$SR_DEV" ]; then
        # Save the associated group.
        G="$(stat -c "%g" "$SR_DEV")"
        [ "$G" -eq 0 ] || echo "$G" >> "$GRPS"
    fi
done

# Print supplementary group IDs.
if [ -n "$(cat "$GRPS")" ]; then
    cat "$GRPS" | grep -v '^$' | grep -v '^0$' | sort -nub | tr '\n' ','
    rm "$GRPS"
else
    rm "$GRPS"
    exit 100
fi

# vim:ft=sh:ts=4:sw=4:et:sts=4
